<?
include_once("../functions/functions.php");
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="print.css">
<style>
body, td {
	font-size: 8pt;
}
.noborder {
	border: 0px solid #ffffff;
}
</style>
<script LANGUAGE="JavaScript">
<!--
function windowPrint() {
	window.print();
	setTimeout("window.close();",1500);
}
 -->
</script>
</head>
<?
// LAST MODIFIED BY SUNIL ON 29/09/03 AT 00:00 HRS

echo"<table width=\"100%\" border=\"0\"><tr>";
$clientID=$HTTP_GET_VARS['selectedClientID'];
?>
<body onload="windowPrint();">
<!-- <body topmargin="0" leftmargin="0" style="font-family: Arial; " bgcolor="#FFFFFF"> -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="normal" width="60%"><b>EMERGENCY RELIEF APPLICATION - Existing Client</b></td>
    <td class="normal" width="13%">
      <div align="center">Ashburton</div>
    </td>
    <td class="normal" width="13%">
      <div align="center">Camberwell</div>
    </td>
    <td class="normal">
      <div align="center">Kew</div>
    </td>
  </tr>
  <tr>
    <td class="normal" width="60%">&nbsp;</td>
    <td colspan="3" class="normal">
      <div align="center" class="small">(please circle)</div>
    </td>
  </tr>
</table>
<?
//session_start();
$client_record = NULL;

	$client_record = getPrintVisitDetails($clientID);
	$client_record = mysql_fetch_array($client_record);
?>
<br />
<table id="AutoNumber1" borderColor="#111111" cellSpacing="0" cellpadding="2" width="100%" border="1">
  <tr>
    <TD colSpan=2>Surname:&nbsp;
      <?  echo "<b>".$client_record['surname']."</b>"; ?>
    </TD>
    <TD colSpan=2>Given Name:&nbsp;
      <?  echo "<b>".$client_record['givenName']."</b>" ;?>
    </TD>
  </tr>
  <TR>
    <TD colSpan="4">Address:&nbsp;
      <?  echo "<b>".$client_record['address']."</b>"; ?>
    </TD>
  </tr>
  <TR>
  	<TD colspan="2">Suburb: &nbsp;
      <?  echo "<b>".$client_record['suburb']."</b>"; ?>
    </TD>
    <TD width="25%">Postcode: &nbsp;
      <?  echo "<b>".$client_record['postcode']."</b>"; ?>
    </TD>
    <TD width="25%">State: &nbsp;
      <?  echo "<b>".$client_record['state']."</b>"; ?>
    </TD>
  </TR>
  <tr>
    <TD colSpan="4">New Address: </TD>
  </tr>
  <TR>
  	<TD colspan="2">New Suburb: &nbsp;
    </TD>
    <TD width="25%">New Postcode: &nbsp;
    </TD>
    <TD width="25%">New State: &nbsp;
    </TD>
  </TR>
  <TR>
    <TD  colSpan=2>Phone Number: &nbsp;
      <?  echo "<b>".$client_record['phone_no']."</b>"; ?>
    </TD>
    <TD  colSpan=2>Mobile Number: <?  echo "<b>".$client_record['mobile_no']."</b>"; ?></td>
  </tr>
  <tr>
    <?
	  	$dobDate = $client_record['dob'];
	  	$dayDOB = NULL;
	  	$monthDOB = NULL;
	  	$yearDOB = NULL;
	  	$DbDOB = NULL;

	  	$DbDOB=explode("-",$dobDate);

	  	@$dayDOB=$DbDOB[2];

	  	@$monthDOB=$DbDOB[1];

	  	$yearDOB=$DbDOB[0];

	  	$dobDate=$dayDOB."/".$monthDOB."/".$yearDOB;
	?>
    <TD width="25%">DOB:&nbsp;
      <?  echo "<b>$dobDate</b>"; ?>
    </TD>
    <TD width="25%">Age:&nbsp;<b>
      <?
				$sql = "SELECT TIMESTAMPDIFF(YEAR, '" . $client_record[ 'dob' ] . "', NOW()) AS Age";
				$ageresult = r_db($sql);
				$age = mysql_fetch_array($ageresult);
				echo $age[0];
  	?>
  	</b>
    </TD>
    <TD  colSpan=2>Gender: &nbsp;
      <?
      if ($client_record['gender'] == "M")
      	$client_record['gender'] = "Male";
      else
      	$client_record['gender'] = "Female";
      echo "<b>".$client_record['gender']."</b>"; ?>
    </TD>
  </TR>
  <TR>
    <TD  colSpan=2>ID Type:&nbsp;
      <?

  	$rowIdType = getFieldsFromLookUpTable("er_Identification_Type","identification_no",$client_record['identification_no']);

  	if(($client_record['other_id_type_specified']) == null )
   			{
     		echo "<b>$rowIdType[1]</b>";
     		}
     else
     		{
     		echo "<b>$rowIdType[1] - ".$client_record['other_id_type_specified']."</b>";
     		}
     	?>
    </TD>
    <TD  colSpan=2>ID Reference:&nbsp;
      <?  echo "<b>".$client_record['id_reference_no']."</SPAN></b>"; ?>
    </td>
  </tr>
  <tr>
    <td colspan="2" >Income Source:</td>
    <td colspan="2" >Income Status:</td>
  </tr>
  <tr>
    <td colspan="2"  >Total Amount:</td>
    <td colspan="2" >When Due :</td>
  </tr>
  <tr>
    <td colspan=2 valign="top">No. of Persons Requesting Assistance:</td>
    <td colspan=1 valign="top">No. of Adult:<br /><span style="font-size: 8pt;">(including children 13 & over on card)</span></td>
    <td colspan=1 valign="top">No. of Children:<br /><span style="font-size: 8pt;">(must be listed on benefit card)</span></td>
  </tr>
  <tr>
    <td width="25%">Housing type:</td>
    <td width="25%">Rent paid pf($):</td>
    <td colspan="2">Reasons for seeking assistance:</td>
  </tr>
</table>
<?
//retrieve visit details
$noOfVisits = getNoOfVisits($clientID);
$totalNoVisits = $noOfVisits;
if ($noOfVisits > 0) {
	// create object
	$nw = new Numbers_Words();

	echo "<p align=\"center\">
					<span style=\"text-decoration:underline\">The total number of visits for this client in the last year was <span style=\"font-weight:bold;\"> $noOfVisits (".$nw->toWords($noOfVisits).")</span>.</span>
					<Br /><span style=\"text-decoration:none\">A list of all client visits is available by viewing the View Visits ERS page.</span></p>";
}
else {
	echo "<br />";
	$noOfVisits++;
}
?>
<table borderColor="#111111" cellSpacing="0" cellPadding="2" width="100%" border="1">
<?
//print visit number
echo "<tr>";
echo "<td width=\"25%\">&nbsp;</td>";
if ($noOfVisits > 4) {
	$noOfVisits = 4;
}
echo "<td nowrap><b>New Visit</b></td>";
for($i=$totalNoVisits;$i>=$totalNoVisits-3;$i--)
{
	if ($i>0) {
		echo "<td >Visit $i</td>";
	}
}
echo "</tr>";
//end print visit number

//print visit date
@$visitDates = getVisitDates($clientID);
$noDates = mysql_num_rows($visitDates);
echo "<tr>";
echo "<td width=\"25%\">Date</td>";
echo "<td>&nbsp;</td>";
for($i=1;$i<=$noDates;$i++)
{
	$currentVisitDate = mysql_fetch_array($visitDates);
	$DbDOB=explode("-",$currentVisitDate['visit_date']);
	@$dayDOB=$DbDOB[2];
	@$monthDOB=$DbDOB[1];
	$yearDOB=$DbDOB[0];
	$dobDate=$dayDOB."/".$monthDOB."/".$yearDOB;
	echo "<td><b>$dobDate</b></td>";
}
echo "</tr>";
//end print visit date

$queryPayment = "SELECT * FROM er_payment";
$resultPayment = r_db($queryPayment);
for($i=1;$i<=$noOfVisits;$i++)
{
	while ($rowPayment = mysql_fetch_array($resultPayment))
	{
		echo "<tr><td valign=\"top\" width=\"25%\">$rowPayment[1] ($)</td>";

		$sql = "SELECT visit_id from er_visit WHERE client_id = $clientID AND visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR) ORDER BY visit_date DESC LIMIT 4";
		$result = r_db($sql);
		$visits = NULL;

		$width = 75/($noOfVisits+1);
		round($width,0);
		echo "<td valign=\"top\" width=\"$width%\" align=\"left\">&nbsp;</td>";

		while ($visits = mysql_fetch_array($result)) {
			$arrVisitDetails = getPaymentDetails1($clientID,$rowPayment['payment_id'], $visits['visit_id']);
			$rowVisit = mysql_fetch_array($arrVisitDetails);
			if ($rowVisit['payment_amount'] > 0) {
				{

					//Print the amount in the appropriate visit coloumn

					$width = 75/($noOfVisits+1);
					round($width,0);
					echo "<td valign=\"top\" width=\"$width%\" align=\"left\">". number_format($rowVisit['payment_amount'],2). "</td>";

				}
			}
			else {
				$width = 75/($noOfVisits+1);
				round($width,0);
				echo "<td valign=\"top\" width=\"$width%\" align=\"left\">&nbsp;</td>";
			}
		}
		echo "</tr>\n";
	}
}
echo "<tr><td width=\"25%\">Total Amount Given ($)</td>";
echo "<td align=\"left\" width=\"$width%\">&nbsp;</td>";
$resultPayment = getTotalAmountOnVisit1($clientID)	;
for($i=1;$i<=$noOfVisits;$i++)
{
	while ($rowErGiven = mysql_fetch_array($resultPayment))
	{
		echo "<td align=\"left\" width=\"$width%\"><b>";
		if ($rowErGiven['pa'] == null)
			echo "0.00";
		else
			echo number_format($rowErGiven['pa'],2);
		echo "</b></td>";
	}
}
echo "</tr>\n";
//finish visit details
?>
    <tr>

	<td width="25%" class="normal">Extra Food Parcels: </td>
	<td class="normal">&nbsp;</td>
  </tr>




</table>
<span class="small"><br /></span>
<table id="AutoNumber3" borderColor="#111111" cellSpacing="0" cellPadding="2" width="100%" border="1">
  <tr>
    <td width="50%"> Cheque Number:</td>
    <td width="50%">Payee:</td>
  </tr>
  <tr>
    <td width="100%" colSpan="2"> Client Referred
      by:</td>
  </tr>
</table>

<?
/* get the checkbox labels */


$emergencyReliefs = get_labels_er("er_Emergency_Relief");

/* create the html code for a formatted set of
checkboxes */
$html_incomes = make_er_checkbox_html($emergencyReliefs, 4, "emergencyReliefs[]");

?>

<tr>
<p align="left"><u>Reason for seeking assistance</p></u>
   <?
      //==================================EMERGENCY RELIEFS===========================
      echo "\n";
      echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";
      echo "$html_incomes";
      echo "</table>\n";
      function make_er_checkbox_html($arr, $num, $name)
      {
      	$closingTR = false;
      	$str = "";
      	$str .= "<tr>\n";
      	if (count($arr) % $num != 0)
      	{
      		$closingTR = true;
      	}

      	$i=1;

      	foreach ($arr as $ele)
      	{
      		$str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->er_id\">";
      		$str .= "$ele->er_reason\n</td>";
      		if($ele->er_reason=="Other")
      		{
      			$str .= "<td>Other Reason:</td>";
      			$str .= "<td><input type=\"text\" name=\"txtOtherEr\" size=\"20\" maxlength=\"50\"></td>";
      		}
      		//$str .= "</tr>";
      		if ($i % $num == 0)
      		{
      			$str .= "</tr>\n<tr>";
      		}
      		else
      		{
      			$str .= "</td>\n";
      		}
      		$i++;
      	}


      	return $str;
      }
?>
<br />

<p align="left"><u>Action taken Associated with E.R. (e.g. referred to other services and agencies)</p></u>



  <?	//==================================ACTION TAKEN===========================
/* get the checkbox labels */


$actionTaken = get_labels_actionTaken("er_action_taken");

/* create the html code for a formatted set of
checkboxes */

$html_action = make_action_checkbox_html($actionTaken, 3, "actionTaken[]");

?>

   <?

      echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">\n";

      echo "$html_action";
      echo "</table>\n";
      function make_action_checkbox_html($arr, $num, $name)
      {
      	$closingTR = false;
      	$str = "";
      	$str .= "<tr>\n";
      	if (count($arr) % $num != 0)
      	{
      		$closingTR = true;
      	}

      	$i=1;

      	foreach ($arr as $ele)
      	{
      		$str .= "<td><input type=\"checkbox\" name=\"$name\" value=\"$ele->action_id\">";
      		$str .= "$ele->action_taken\n</td>";
      		if($ele->action_taken=="Other (financial only)")
      		{
      			$str .= "<td>Other Reason:</td>";
      			$str .= "<td><input type=\"text\" name=\"txtActionOther\" size=\"20\" maxlength=\"50\"></td>";
      		}
      		//$str .= "</tr>";
      		if ($i % $num == 0)
      		{
      			$str .= "</tr>\n<tr>";
      		}
      		else
      		{
      			$str .= "</td>\n";
      		}
      		$i++;
      	}


      	return $str;
      }
?>


<table id="AutoNumber3" cellSpacing="0" cellPadding="6" width="100%" border="0" class="noborder">
  <tr valign="top">
    <td width="33%" class="noborder">
      <div align="center">Interviewer's Name</div>
    </td>
    <td width="33%" class="noborder">
      <div align="center">Client's Signature</div>
    </td>
    <td class="noborder">
      <div align="center">Date of Visit</div>
    </td>
  </tr>
  <tr>
    <td width="33%" class="noborder">
      <div align="center">.....................................</div>
    </td>
    <td width="33%" class="noborder">
      <div align="center">.....................................</div>
    </td>
    <td class="noborder">
      <div align="center">.....................................</div>
    </td>
  </tr>
</table>
<div align="left">Privacy: Camcare respects your privacy. Please ask a staff member to show you the Privacy Policy</div>
</body>
</html>
<?
function getNoOfVisits($clientID)
{
	$queryNoOfVisits = "SELECT er_Client.client_id, COUNT(er_visit.visit_id)
						FROM er_Client, er_visit
						WHERE er_Client.client_id=er_visit.client_id
						AND visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						AND er_Client.client_id='$clientID'
						GROUP BY er_Client.client_id";
	$resultNoOfVisits = r_db($queryNoOfVisits);
	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	return $noOfVisits;

}

function getPaymentDetails1($ClientID, $rowPayment, $visitPaymentID)
{
	$arr = array();
	$queryVisit = "SELECT a.payment_amount
				FROM er_Payment_On_Visit as a INNER JOIN er_Visit as b ON b.visit_id = a.visit_id
				WHERE b.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
					AND a.payment_id=$rowPayment
					AND b.visit_id = $visitPaymentID
					ORDER BY b.visit_date, b.visit_id";
	//echo $queryVisit;
	$resultVisit = r_db($queryVisit);
	return $resultVisit;
}

function getVisitDates($ClientID)
{
	$queryVisit = "SELECT visit_date, visit_id
						  FROM er_visit
						  WHERE visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						  AND client_id= $ClientID
						  ORDER BY er_visit.visit_date desc
						  LIMIT 4";
	//echo $queryVisit;
	$resultVisit = r_db($queryVisit);
	return $resultVisit;
}

function getPrintVisitDetails($clientID)
{
$queryVisitDate = "SELECT er_Client.*,
						  er_visit.visit_id,
						  er_visit.visit_date,
						  er_visit.site_visited_id,
						  er_visit.interviewer,
	  					  er_visit.action_taken,
						  er_visit.day_book_colour,
						  er_visit.day_book_page_no,
						  er_visit.no_children_assistted,
						  er_visit.no_adults_assistted,
						  er_visit.address,
						  er_visit.suburb,
						  er_visit.state,
						  er_visit.postcode,
						  er_housing_type.housing_type,
						  er_visit.phone_no,
						  er_income_status.income_status_type,
						  er_visit.other_housing_type,
						  er_visit.centrelink_loan_amt_borrowed,
						  er_visit.rent_paid_pf,
						  er_agency.agency_name,
						  er_visit.other_income_status,
						er_Client.id_reference_no,
						er_Client.surname,
						er_Client.givenname,
						er_Client.dob,
						er_Client.nationality,
						er_Client.gender,
						er_Client.preferred_language,
						er_Client.other_id_type_specified,
						er_visit.mobile_no,
						er_Client.identification_no,
						er_visit.extra_food_parcels
						  FROM er_Client, er_visit,er_housing_type,er_income_status,er_agency
						  WHERE er_Client.client_id=er_visit.client_id
						  AND er_Client.client_id='$clientID'
						  AND er_visit.housing_type_id=er_housing_type.housing_type_id
						  AND er_visit.income_status_id=er_income_status.income_status_id
						  AND er_visit.agency_id=er_agency.agency_id
						  ORDER BY er_visit.visit_date desc, er_visit.visit_id desc
						  LIMIT 1";
//echo $queryVisitDate;
$resultVisitDate = r_db($queryVisitDate);
return $resultVisitDate;
}

function getSite($siteID)
{
	$queryNoOfVisits = "SELECT er_site.site_name
					FROM er_site
					WHERE er_site.site_id=$siteID" ;
	$resultsite = r_db($queryNoOfVisits);
	$rowNoOfsite = mysql_fetch_array($resultsite);
	$resultsite = $rowNoOfsite[0];
	return $resultsite;
}


function getTotalAmountOnVisit1($ClientID)
{
	$queryErGiven = "SELECT SUM(payment_amount) as pa, er_visit.visit_id
						FROM er_Client, er_visit, er_Payment_On_Visit
						WHERE er_Client.client_id=er_visit.client_id
						AND er_visit.visit_id=er_Payment_On_Visit.visit_id
						AND er_Client.client_Id = '$ClientID'
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						GROUP BY er_visit.visit_date DESC, er_visit.visit_id
						LIMIT 4";

	$queryErGiven = "SELECT SUM(er_Payment_On_Visit.payment_amount) as pa, er_visit.visit_id
						FROM er_Client INNER JOIN er_visit on er_Client.client_id=er_visit.client_id LEFT OUTER JOIN er_Payment_On_Visit on er_visit.visit_id=er_Payment_On_Visit.visit_id
						WHERE er_Client.client_Id = '$ClientID'
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						GROUP BY er_visit.visit_date DESC, er_visit.visit_id
						LIMIT 4";
	//echo $queryErGiven;
	$resultErGiven = r_db($queryErGiven);
	return $resultErGiven;
}
?>
